<?php

class BTItemsDAO {
    private $db;

    public function  __construct() {
        $this->db = new DB();
    }

    public function  __destruct() {
        $this->db->Disconnect();
    }

    public function GetAllItems( $perpage=PERPAGE,$cpage=1 ){
        $sql = "SELECT * FROM `btitems` ORDER BY id DESC";
        return $this->getRows($sql, PERPAGE, $cpage );
    }

    public function GetAllItemCount(){
        $sql = "SELECT count(*) FROM `btitems`";
        return $this->db->GetValue( $sql );
    }

    public function GetItemById( $itemId ){
        $sql = "SELECT * FROM `btitems` WHERE id=$itemId";
        return $this->db->getRow( $sql );
    }

    public function GetItemsByCategoryId( $cid, $perpage=PERPAGE,$cpage=1, $isIndex=false ){
        if( $isIndex ){
            $sql = "SELECT * FROM `btitems` WHERE category=$cid and imgurl <> '' order by id desc";
        }
        else{
            $sql = "SELECT * FROM `btitems` WHERE category=$cid order by id desc";
        }
        return $this->getRows($sql, $perpage, $cpage );
    }

    public function GetItemsCountByCategoryId( $cid ){
        $sql = "SELECT count(*) FROM `btitems` WHERE category=$cid";
        return $this->db->GetValue( $sql );
    }

    /**
     * Get related post by category Id
     * @param <type> $cid
     */
    public function GetRandomPostByCategory( $cid ){
        // Get Latest posts
        $sql = "select * from btitems WHERE category=$cid AND RIGHT(id,2) = RIGHT(RAND(),2) ORDER BY id desc limit 0,10;";
        return $this->db->GetPlan( $sql );
    }

    /**
     * 搜索数据库操作
     * 
     * @param <type> $keyword
     * @param <type> $perpage
     * @param <type> $cpage
     * @return <type>
     */
    public function DoSearch( $keyword, $perpage=PERPAGE,$cpage=1 ){
        $result = array();
        $sql = "SELECT count(*) from `btitems` WHERE title like '%$keyword%'";
        $result['count'] = $this->db->GetValue( $sql );

        $sql = "SELECT * from `btitems` WHERE title like '%$keyword%'";
        $result['items'] = $this->getRows($sql, PERPAGE, $cpage );

//        print_r( $result );

        return $result;
    }
    
    public function UpdateItemImgUrl( $itemId, $imgUrl ){
        $sql = "UPDATE `btitems` set imgurl='$imgUrl' WHERE id=$itemId";
        try{
            $result = $this->db->Update($sql);
            return $result;
        }
        catch( DBException $exception ){
            return false;
        }
    }
    
    /**
     * update item detail information
     * 
     * @param type $itemId
     * @param type $largeimgurl
     * @param type $doubanrating
     * @param type $doubansubtype
     * @param type $doubanid
     * @param type $year
     * @param type $originaltitle
     * @return boolean 
     */
    public function UpdateItemDetail( $itemId, $largeimgurl, $doubanrating, $doubansubtype, $doubanid, $year, $originaltitle ) {
        $sql = "UPDATE `btitems` set largeimgurl='$largeimgurl', 
            doubanrating='$doubanrating',
            doubansubtype='$doubansubtype',
            doubanid = $doubanid,
            year = $year,
            originaltitle='$originaltitle'
            WHERE id=$itemId";
        
        try{
            $result = $this->db->Update($sql);
            return $result;
        }
        catch( DBException $exception ){
            return false;
        }
    }
    
    public function UpdateItemViewCount( $itemId ){
        $sql = "UPDATE `btitems` set viewcount=viewcount+1 WHERE id=$itemId";
        try{
            $result = $this->db->Update($sql);
            return $result;
        }
        catch( DBException $exception ){
            return false;
        }
    }
    
    /**
     * 获得最近更新的资源
     * 
     * @param type $cids
     * @param type $perpage
     * @param type $cpage
     * @return type 
     */
    public function GetLatestItems( $cids, $perpage=PERPAGE,$cpage=1 ){
        // Get Latest posts
        $strIds = implode(',', $cids);
        $sql = "select DISTINCT(title) as title, id, imgurl, magurl, edkurl from btitems WHERE category in ( $strIds ) order by id desc";
        return $this->getRows( $sql, PERPAGE, $cpage );
    }
    
    /**
     * 
     * 获得最热的资源
     * 
     * @param type $cids
     * @param type $perpage
     * @param type $cpage
     * @return type 
     */
    public function GetHotestItems( $cids, $perpage=PERPAGE,$cpage=1 ){
        // Get Latest posts
        $strIds = implode(',', $cids);
        $sql = "select DISTINCT(title) as title, id, imgurl, magurl, edkurl from btitems WHERE category in ( $strIds ) order by viewcount desc";
        return $this->getRows( $sql, PERPAGE, $cpage );
    }

    /**
     * get limited result set by page
     * @param string $sql
     * @param int $perpage
     * @param int $cpage current page
     * @return array
     */
    protected function getRows($sql,$perpage,$cpage=1,$function="GetPlan") {
        $start = ($cpage-1) * $perpage;
        $end = $perpage;

        $sql .= " LIMIT $start,$end";

        //print $sql;
        //die();
        $result = $this->db->$function($sql);
        return $result;
    }
}

?>
